package android.explorer.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.explorer.R;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
class ProgressThread extends Thread {
    Handler mHandler;
    final static int STATE_DONE = 0;
    final static int STATE_RUNNING = 1;
    String src, des;
    int mState,total=0,counting;
    Activity parent;
	private void startCopy(String src,String des){
		System.out.println(src+"     to   "+des);
		if(mState != STATE_RUNNING)return;
		File fs=new File(src);
		File fd=new File(des);
		if(fs.isFile()){
			Message msg = mHandler.obtainMessage();
            Bundle b = new Bundle();
            b.putString("current", fs.getName());
            b.putInt("total", total);
            b.putInt("count", ++counting);
            msg.setData(b);
            mHandler.sendMessage(msg);
			if(!copyfile(fs,fd)){
				Toast.makeText(parent, "Copying failed !", Toast.LENGTH_LONG).show();
				mState=STATE_DONE;
			}else{
				
			}
		}else{
			if(!fd.exists() & !fd.mkdir()){
				Toast.makeText(parent, "Create directory failed !", Toast.LENGTH_LONG).show();
				mState=STATE_DONE;
			}
			for(String str:fs.list()){
				if(mState != STATE_RUNNING)return;
				startCopy(src+"/"+str,des+"/"+str);
			}
		}
	}
	private boolean copyfile(File f1,File f2){
		System.out.println(f1.getAbsolutePath()+" --> "+f2.getAbsolutePath());
		try{
			InputStream in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0){
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			System.out.println("Copy OK");
			return true;
		}catch(Exception ex){
			System.out.println("Copy failed");
			return false;
		}
	}
	private void preparing(File file){
		if(file.list()!=null)
		for(String str:file.list()){
			File f =new File(file.getAbsolutePath()+"/"+str);
			if(f.isFile())total++;
			else if(f.isDirectory())preparing(f);
		}
	}
    public void run() {
        mState = STATE_RUNNING;   
        preparing(new File(src));
        if(total==0)total=1;
        startCopy(src,des);
        parent.finish();
    }
    public void setState(int state) {
        mState = state;
    }
    public ProgressThread(Handler h,String src,String des,Activity act){
        mHandler = h;
        this.src=src;
        this.des=des;
        parent=act;
    }
}
public class FileCopy extends Activity {
	boolean stop=false;
	String src,des;
	TextView tvfile,tvsrc,tvdes,tvcur,tvpro;
	ProgressThread progressThread;
	ProgressBar progressBar;
	protected void onStart() {
		super.onStart();
		progressBar.setMax(100);
		progressBar.setScrollBarStyle(ProgressDialog.STYLE_HORIZONTAL);
		System.out.println("Start copy");
        progressThread = new ProgressThread(handler,src,des,this);
        progressThread.start();

	}
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            tvcur.setText("Current : "+msg.getData().getString("current"));
            int total=msg.getData().getInt("total");
            progressBar.setMax(total);
            int progress=msg.getData().getInt("count");
            progressBar.setProgress(progress);
            System.out.println(progress+"   "+total);
            tvpro.setText(" "+(100*progress/total)+"% done.    "+progress+"/"+total+" file(s)");
        }
    };
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.copyfile);
		Intent intent=getIntent();
		src=intent.getExtras().getString("filesource");
		des=intent.getExtras().getString("folderdes");
		
		File file=new File(src);
		File fdes=new File(des);
		
		tvfile=(TextView)findViewById(R.id.copyfilename);
		tvsrc=(TextView)findViewById(R.id.copyfilefrom);
		tvdes=(TextView)findViewById(R.id.copyfileto);
		tvpro=(TextView)findViewById(R.id.copyfilepro);
		tvcur=(TextView)findViewById(R.id.copycur);
		
		tvfile.setText("Copy file : " +file.getName());
		tvsrc.setText("From : "+file.getParent());
		tvdes.setText("To : "+fdes.getParent());
		tvcur.setText("Current : ");
		progressBar=(ProgressBar)findViewById(R.id.copyProgress);
	}
	
	public void onStopButtonClick(View target) {
		progressThread.mState=ProgressThread.STATE_DONE;
		stop=true;
		finish();
	}

}
